/*
 * ImmediateCrypt
 * Copyright (C) 2012 Giacomo Drago
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * http://giacomodrago.com/go/immediatecrypt
 * 
 */

package com.giacomodrago.utils.gui;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import javax.swing.JDialog;

@SuppressWarnings("serial")
class UncaughtExceptionReporter extends JDialog {

    protected final String stackTrace;
    
    /** Creates new form UncaughtExceptionReporter */
    public UncaughtExceptionReporter(String stackTrace) {
        this.stackTrace = stackTrace;
        initComponents();
        txtExceptionInfo.setCaretPosition(0);
        getRootPane().setDefaultButton(buttonIgnore);
        buttonIgnore.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelMessage = new javax.swing.JLabel();
        scrollPane = new javax.swing.JScrollPane();
        txtExceptionInfo = new javax.swing.JTextArea();
        buttonCopyDebugInfo = new javax.swing.JButton();
        buttonTerminate = new javax.swing.JButton();
        buttonIgnore = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Unexpected error");
        setAlwaysOnTop(true);
        setModal(true);
        setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE);

        labelMessage.setText("<html><b>An unexpected error happened during program runtime.</b><br /><br />Some debug information follows:</html>");
        labelMessage.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        txtExceptionInfo.setColumns(20);
        txtExceptionInfo.setEditable(false);
        txtExceptionInfo.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        txtExceptionInfo.setRows(5);
        txtExceptionInfo.setText(this.stackTrace);
        scrollPane.setViewportView(txtExceptionInfo);

        buttonCopyDebugInfo.setText("Copy debug info");
        buttonCopyDebugInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCopyDebugInfoActionPerformed(evt);
            }
        });

        buttonTerminate.setFont(buttonTerminate.getFont());
        buttonTerminate.setText("Terminate program");
        buttonTerminate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTerminateActionPerformed(evt);
            }
        });

        buttonIgnore.setFont(buttonIgnore.getFont().deriveFont(buttonIgnore.getFont().getStyle() | java.awt.Font.BOLD));
        buttonIgnore.setText("Ignore and continue");
        buttonIgnore.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonIgnoreActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrollPane)
                    .addComponent(labelMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(buttonCopyDebugInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonTerminate, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonIgnore, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonIgnore)
                    .addComponent(buttonTerminate)
                    .addComponent(buttonCopyDebugInfo))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonTerminateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTerminateActionPerformed
        System.exit(-1);
    }//GEN-LAST:event_buttonTerminateActionPerformed

    private void buttonCopyDebugInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCopyDebugInfoActionPerformed
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        StringSelection clipboardContents = new StringSelection(txtExceptionInfo.getText());
        clipboard.setContents(clipboardContents, null);
    }//GEN-LAST:event_buttonCopyDebugInfoActionPerformed

    private void buttonIgnoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonIgnoreActionPerformed
        this.dispose();
    }//GEN-LAST:event_buttonIgnoreActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCopyDebugInfo;
    private javax.swing.JButton buttonIgnore;
    private javax.swing.JButton buttonTerminate;
    private javax.swing.JLabel labelMessage;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTextArea txtExceptionInfo;
    // End of variables declaration//GEN-END:variables
}
